package com.kkyy.msystem.controller;

import com.kkyy.msystem.entity.UsersEntity;
import com.kkyy.msystem.service.UsersService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    UsersService usersService;

    @RequestMapping({"/", "/login"})
    public String login() {
        return "login";
    }

    // 后台登陆
    @RequestMapping("/dologin")
    @ResponseBody
    public String dologin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        // 根据账号查用户
        UsersEntity user = usersService.getUserByUsername(username);
        // 判断密码
        SimpleHash md5 = new SimpleHash("MD5", password, username);
        if (user == null) {
            return "用户不存在！";
        }
        if (password == "") {
            return "请输入密码！";
        }
        if (md5.toString().equals(user.getPassword())) {
            session.setAttribute(UsersEntity.SESSION_USER_KEY, user);
            return "/msystem/index";
        } else {
            return "账号或密码错误！";
        }
    }
}